import {
  Component,
  ViewChild,
  Injector,
  Output,
  EventEmitter,
  ElementRef,
} from "@angular/core";
import { ModalDirective } from "ngx-bootstrap";
import { AppComponentBase } from "@shared/common/app-component-base";

import * as moment from "moment";
import { finalize } from "rxjs/operators";
import {
  MainTaskServiceProxy,
  TaskDto,
  MainTaskGoProductionDto,
  NameValueDtoOfNameValueDto,
  PipelineServiceProxy,
  SideTaskServiceProxy,
  MonthPlanServiceProxy,
} from "@shared/service-proxies/service-proxies";
import { LazyLoadEvent } from "primeng/api";

@Component({
  selector: "setWorkshopremark",
  templateUrl: "./set-workshopremark.component.html",
})
export class SetWorkshopremarkComponent extends AppComponentBase {
  @ViewChild("actionModal") modal: ModalDirective;
  @ViewChild("tmplCombobox") tmplCombobox: ElementRef;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
  active = false;
  saving = false;
  monthId: string;
  workShopRemark: string;
  event?: LazyLoadEvent;
  type: number;
  constructor(
    injector: Injector,
    private _monthServiceProxy: MonthPlanServiceProxy
  ) {
    super(injector);
  }
  show(
    id: string,
    workShopRemark: string,
    type: number,
    event?: LazyLoadEvent
  ): void {
    this.monthId = id;
    this.workShopRemark = workShopRemark;
    this.type = type;
    this.event = event;
    this.active = true;
    this.modal.show();
  }
  save(): void {
    this.saving = true;
    if (this.type == 1) {
      this._monthServiceProxy
        .setWorkShopRemark(this.monthId, this.workShopRemark)
        .pipe(
          finalize(() => {
            this.saving = false;
          })
        )
        .subscribe((result) => {
          this.notify.info("录入成功");
          this.close();
          this.modalSave.emit(this.event);
        });
    } else if (this.type == 2) {
      this._monthServiceProxy
        .setToolingRemark(this.monthId, this.workShopRemark)
        .pipe(
          finalize(() => {
            this.saving = false;
          })
        )
        .subscribe((result) => {
          this.notify.info("录入成功");
          this.close();
          this.modalSave.emit(this.event);
        });
    } else if (this.type == 3) {
      this._monthServiceProxy
        .setMaterialRemark(this.monthId, this.workShopRemark)
        .pipe(
          finalize(() => {
            this.saving = false;
          })
        )
        .subscribe((result) => {
          this.notify.info("录入成功");
          this.close();
          this.modalSave.emit(this.event);
        });
    }
  }

  close(): void {
    this.saving = false;
    this.modal.hide();
  }
}
